package cn.bytecollege.user.controller;

import cn.bytecollege.common.constant.Resource;
import cn.bytecollege.common.entity.User;
import cn.bytecollege.common.util.MD5Util;
import cn.bytecollege.user.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

@WebServlet(name = "UserRegisterController",value = "/user/register")
public class UserRegisterController extends HttpServlet {
    private UserService userService = null;
    public  UserRegisterController(){
        userService = new UserService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/user/userRegister.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收form表单传的数据
        String username= request.getParameter("username");
        String password = request.getParameter("password");
        String phonenumber = request.getParameter("phonenumber");
        String headImg = request.getParameter("headImg");
        String nickname = request.getParameter("nickname");
        String gender = request.getParameter("gender");

        //封装数据
        User user = new User();
        user.setUsername(username);
        user.setUserPassword(MD5Util.md5Encode(password));
        user.setUserPhonenumber(phonenumber);
        user.setUserHeadPortrait(headImg);
        user.setCreateTime(LocalDateTime.now());
        user.setUserNickname(nickname);
        user.setUserGender(gender);


        userService.save(user);
        //注册成功后跳转到登录
        response.sendRedirect("/user/login");
    }
}
